package com.aurora.source.center.activity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.BaseActivity;
import com.aurora.source.base.BaseRecyclerAdapter;
import com.aurora.source.center.adapter.FlowAdapter;
import com.aurora.source.center.bean.PersonFlowBean;
import com.aurora.source.center.presenter.SourceCenterMainPresenter;
import com.aurora.source.center.view.SourceCenterMainView;
import com.aurora.source.entity.UserEntity;
import com.aurora.source.event.EventData;
import com.aurora.source.source.activity.SettingActivity;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.widget.CircleImageView;
import com.aurora.source.widget.SimpleToolbar;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author: ww
 * @description:
 * @date: 2018/9/6
 */
public class SourceCenterMainActivity extends BaseActivity<SourceCenterMainPresenter> implements SourceCenterMainView {

   /* @BindView(R.id.wv_flow)
    RxRulerWheelView wvFlow;*/

    @BindView(R.id.rv_flow)
    RecyclerView rvFlow;
    @BindView(R.id.resizableImageView)
    ImageView resizableImageView;
    @BindView(R.id.stb_title)
    SimpleToolbar stbTitle;
    @BindView(R.id.iv_collect)
    ImageView ivCollect;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_authority)
    TextView tvAuthority;
    int lastSelectPosition;
    FlowAdapter flowAdapter;
    PersonFlowBean selectData;
    @BindView(R.id.tv_collect)
    TextView tvCollect;
    @BindView(R.id.iv_source)
    ImageView ivSource;
    @BindView(R.id.tv_source)
    TextView tvSource;
    List<PersonFlowBean> flowList;//流程权限
    @BindView(R.id.tv_left_title)
    TextView tvLeftTitle;
    @BindView(R.id.tv_main_title)
    TextView tvMainTitle;
    @BindView(R.id.tv_right_title)
    TextView tvRightTitle;
    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.ll_home)
    LinearLayout llHome;
    @BindView(R.id.ll_source)
    LinearLayout llSource;
    @BindView(R.id.iv_setting)
    ImageView ivSetting;
    @BindView(R.id.tv_setting)
    TextView tvSetting;
    @BindView(R.id.ll_setting)
    LinearLayout llSetting;

    @Override
    public SourceCenterMainPresenter attach() {
        return new SourceCenterMainPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_source_center_main;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        EventBus.getDefault().register(this);
        UserEntity userEntity = GreenDaoHelper.getDaoSession().getUserEntityDao().loadAll().get(0);
        Glide.with(this)
                .load(userEntity.getUserHeadPath())
                .apply(new RequestOptions().placeholder(R.drawable.ico_txiang01))
                .into(ivHead);
        tvName.setText(userEntity.getUserName());
        tvAuthority.setText(userEntity.getProcessTitles());
        stbTitle.setMainTitle("溯源中心");
        stbTitle.setLeftVisibility(View.VISIBLE);
        stbTitle.setLeftTitleClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftInput();
                finish();
            }
        });
        stbTitle.setRightTitleText("操作记录");
        stbTitle.setRightVisibility(View.VISIBLE);
        flowAdapter = new FlowAdapter();
        flowAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener<PersonFlowBean>() {
            @Override
            public void onClick(int position, PersonFlowBean data) {

                if (data.isHavaPermision()) {
                    selectData = data;
                    ivCollect.setVisibility(View.VISIBLE);
                    tvCollect.setVisibility(View.VISIBLE);
                    stbTitle.setRightVisibility(View.VISIBLE);
                }

            }
        });
        rvFlow.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvFlow.setAdapter(flowAdapter);

        tvSource.setTextColor(Color.parseColor("#5DD189"));
        ivSource.setImageResource(R.drawable.ico_nav_suy1);
    }

    //接收退出登录操作发过来的消息  执行退出应用
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void helloEventBus(String message) {
        if (message.equals(EventData.logOut)) {
            hideSoftInput();
            finish();
        }
    }

    @Override
    public void doBusiness() {
        basePresenter.getFlowInfo(userId);
    }


    @OnClick({R.id.ll_home, R.id.ll_setting, R.id.tv_right_title, R.id.iv_collect})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_home:
                hideSoftInput();
                finish();
                break;
            case R.id.ll_setting:
                gotoActivity(SettingActivity.class, true);
                break;
            case R.id.tv_right_title://操作记录
                if (selectData == null) {
                    showToast("暂无权限");
                    return;
                }
                Bundle bundle1 = new Bundle();
                bundle1.putSerializable("flowList", (Serializable) flowList);
                bundle1.putSerializable("currentFlow", selectData);
                gotoActivity(OperationRecoredActivity.class, false, bundle1);
                break;
            case R.id.iv_collect: //数据采集
                if (selectData == null) {
                    showToast("暂无权限");
                    return;
                }
                String[] item = new String[]{"新增主记录", "添加子记录"};
                AlertDialog.Builder appCompatDialog = new AlertDialog.Builder(this);
                appCompatDialog.setSingleChoiceItems(item, -1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        if (which == 0) { //主流程
                            Bundle bundle = new Bundle();
                            bundle.putString("flowPosition", selectData.getProcessNumber());
                            bundle.putString("processId", selectData.getFlowId());
                            gotoActivity(AddFlowActivity.class, false, bundle);
                        } else if (which == 1) { //子流程
                            Bundle bundle1 = new Bundle();
                            bundle1.putSerializable("flowList", (Serializable) flowList);
                            bundle1.putSerializable("currentFlow", selectData);
                            gotoActivity(OperationRecoredActivity.class, false, bundle1);
                        }
                    }
                });
                appCompatDialog.create().show();
                break;
        }
    }

    @Override
    public void onGetFlowFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onGetFlowSuccess(List<PersonFlowBean> personFlowBeans) {
        flowList = personFlowBeans;
        flowAdapter.setDataList(flowList);
        if (personFlowBeans != null) {
            for (int i = 0; i < personFlowBeans.size(); i++) {
                if (personFlowBeans.get(i).isHavaPermision()) {
                    flowAdapter.setSelectBean(i);//选中第一个
                    selectData = personFlowBeans.get(i);
                    return;
                }
            }
        }


    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
